﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using RentalSystem.Data.EF;
using Web.Models;
using System.Globalization;
using System.Text;

namespace Web.Helper
{
    public static class Helper
    {
        public static string GenerateGuid()
        {
            return Guid.NewGuid().ToString("N");
        }

        public static string GenerateRandomPassword(int length)
        {
            StringBuilder password = new StringBuilder();
            Random rand = new Random();
            char[] characters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '1', '2', '3', '4', '5', '6', '7' };


            for (int i = 0; i < length; i++)
            {
                int index = rand.Next(characters.Length - 1);
                password.Append(characters[index]);
            }

            return password.ToString();
        }

        public static string ReplaceSpaceAndSpecialChar(string text)
        {
            text = text.Replace(' ', '_');
            text = text.Replace('ą', 'a');
            text = text.Replace('ś', 's');
            text = text.Replace('ć', 'c');
            text = text.Replace('ź', 'z');
            text = text.Replace('ż', 'z');
            text = text.Replace('ń', 'n');
            text = text.Replace('ó', 'o');

            return text;
        }

        public static void ResizeImageAndSave(string filePath, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(filePath, ImageFormat.Jpeg);
            image.Dispose();
            newImage.Dispose();
        }

        public static List<Movies> FilterMovies(List<Movies> movies, MovieSearchViewModel filters)
        {
            if (movies == null || movies.Count == 0 || filters == null)
                return null;

            List<Movies> result = new List<Movies>();

            DateTime from = DateTime.Now;
            DateTime to = DateTime.Now;

            if (String.IsNullOrEmpty(filters.titleContains) &&
                String.IsNullOrEmpty(filters.directorContains) &&
                String.IsNullOrEmpty(filters.producerContains) &&
                String.IsNullOrEmpty(filters.dateFrom) &&
                String.IsNullOrEmpty(filters.dateTo))
                return result;


            if (!String.IsNullOrEmpty(filters.dateFrom) && !DateTime.TryParse(filters.dateFrom, out from))
                throw new Exception("Niepoprawny format daty");
            if (!String.IsNullOrEmpty(filters.dateTo) && !DateTime.TryParse(filters.dateTo, out to))
                throw new Exception("Niepoprawny format daty");

            foreach (Movies movie in movies)
            {
                try
                {
                    if (!String.IsNullOrEmpty(filters.titleContains))
                    {
                        if (!movie.MovieTitle.ToLower().Contains(filters.titleContains.ToLower())) continue;
                    }
                    if (!String.IsNullOrEmpty(filters.directorContains))
                    {
                        if (!movie.MovieDirector.ToLower().Contains(filters.directorContains.ToLower())) continue;
                    }
                    if (!String.IsNullOrEmpty(filters.producerContains))
                    {
                        if (!movie.MoviePublisher.ToLower().Contains(filters.producerContains.ToLower())) continue;
                    }
                    if (!String.IsNullOrEmpty(filters.dateFrom))
                    {
                        if (from == null) continue;
                        if (movie.MovieIssueDate < from) continue;
                    }
                    if (!String.IsNullOrEmpty(filters.dateTo))
                    {
                        if (to == null) continue;
                        if (movie.MovieIssueDate > to) continue;
                    }

                    result.Add(movie);
                }
                catch { }
            }

            return result;
        }

        public static string LogTypeToString(LogType type)
        {
            switch (type)
            {
                case LogType.ActivateCustomer: return "Aktywacja klienta";
                case LogType.AddLocation: return "Dodanie lokalizacji odbioru";
                case LogType.AddMovie: return "Dodanie filmu";
                case LogType.AddUser: return "Dodanie użytkownika";
                case LogType.Comment: return "Nowy komentarz";
                case LogType.DeleteLocation: return "Usunięcie agencji";
                case LogType.DeleteMovie: return "Usunięcie filmu";
                case LogType.DeleteUser: return "Usunięcie użytkownika";
                case LogType.EditLocation: return "Edycja lokalizacji";
                case LogType.EditMovie: return "Edycja filmu";
                case LogType.EditMovieCategories: return "Edycja kategorii filmu";
                case LogType.EditMovieDiscs: return "Edycja nośników";
                case LogType.EditPageContent: return "Edycja zawartości serwisu";
                case LogType.EditUser: return "Edycja użytkownika";
                case LogType.Error: return "Nieznany błąd";
                case LogType.Login: return "Zalogowanie do serwisu";
                case LogType.Logout: return "Wylogowanie z serwisu";
                case LogType.ReceiveReturn: return "Obriór nośnika";
                case LogType.Rent: return "Wypożyczenie nośnika z filmem";
                case LogType.Return: return "Zwrot nośnika";
                case LogType.SelectLocation: return "Wybór lokalizacji odbioru";
                case LogType.SignIn: return "Założenie konta";
                case LogType.SingOut: return "Usunięcie Konta";
                case LogType.Vote: return "Oddanie głosu na film";
                default: return "Unknown";
            }
        }
    }
}